<?php
namespace models\admin;

use libs\request as request;

class tresci extends \models\_SkyCMSModel
{
    
    private $_tabela = 'strony';
    
    /**
     * @route=(/)
     */
    public function index()
    {
        $allnews = $this->database->query()->getAll($this->_tabela);
        
        return array('allnews'=>$allnews);
    }
    
    
    /**
     * @route=(/tresci/dodaj)
     */
    public function dodaj()
    {
        $result = $this->database->query()->insert($this->_tabela,array(
                                                            'jezyk'=>request::post('jezyk'),
                                                            'priorytet'=>request::post('priorytet'),
                                                            'nazwa'=>request::post('nazwa'),
                                                            'tresc'=>request::post('tresc')
                                                        ));
        
        header('Location: '.URL.'_SkyCMS/');
    }
    
    
    /**
     * @route=(/tresci/edytuj/{id})
     */
    public function edytuj($array)
    {
        $id = $array[0];
        
        try{
            
            $news = $this->database->query()->get_where($this->_tabela, array('id'=>$id));
            
            if($news == null)
            {
                throw new \Exception('Nie znaleziono rekordu');
            }
            
        }
        catch(\Exception $e)
        {
            echo $e->getMessage();
            die();
        }
        
        return array('news'=>$news);
    }
    
    
    /**
     * 
     * @route=(/tresci/zapisz/{id})
     */
    public function zapisz($array)
    {
        $id = $array[0];
        
        $result = $this->database->query()->update($this->_tabela,
                                                    array(
                                                        'data_modyfikacji'=>date('Y-m-d H:i:s'),
                                                        'jezyk'=>request::post('jezyk'),
                                                        'priorytet'=>request::post('priorytet'),
                                                        'nazwa'=>request::post('nazwa'),
                                                        'tresc'=>request::post('tresc')
                                                    ),
                                                    array(
                                                        'id'=>$id
                                                    ));
        
        return array();
    }
    
    
    /**
     * @route=(/tresci/usun/{id})
     */
    public function usun($array)
    {
        $id = $array[0];
        
        $result = $this->database->query()->delete($this->_tabela,array('id'=>$id));
        
        header('Location: '.URL.'_SkyCMS');
    }
}

?>
